package com.cac.demo.newCode.cemop.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cac.demo.controller.compressedFiles.CompressedFilesEditor;
import com.cac.demo.newCode.cemop.dto.*;
import com.cac.demo.newCode.cemop.enums.CemopEnum;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class CemopUtil {
    @Resource
    private CompressedFilesEditor compressedFilesEditor;
    @Resource
    private Environment environment;

    /**
     * 下载Cemop文件处理
     *
     * @param jsonObject
     * @return
     */
    public CemopDownDTO downUtil(@RequestBody JSONObject jsonObject, String type) {
        //文件传输时DTO
        CemopDownDTO cemopDownDTO = new CemopDownDTO();
        log.info("=========================Cemop文件传输===========================" + jsonObject);
        String fileUrl = jsonObject.get("fileUrl").toString();
        log.info("=========================下载zip文件并解析===========================");
        String str = String.valueOf(compressedFilesEditor.parseJsonFromZip(fileUrl));
        log.info("==========================文件内容===========================" + str);
        if (str.length() < 20) {
            log.info("压缩文件为空");
            cemopDownDTO.setMessage("解析失败" + str);
            cemopDownDTO.setStatus("fail");
            return cemopDownDTO;
        } else {
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                JsonNode json = objectMapper.readTree(str);
                cemopDownDTO.setData(json);
                //根据类型将返回值转为DTO
                if (type.equals(CemopEnum.RETURN_ORDER.getCode())) {
                    List<CemopReturnOrderDTO> list = objectMapper.readerForListOf(CemopReturnOrderDTO.class).readValue(str);
                    cemopDownDTO.setCemopReturnOrderDTOList(list);
                    return cemopDownDTO;
                } else if (type.equals(CemopEnum.OUTBOUND.getCode())) {
                    List<CemopOutboundDTO> list = objectMapper.readerForListOf(CemopOutboundDTO.class).readValue(str);
                    cemopDownDTO.setCemopOutboundDTOS(list);
                    return cemopDownDTO;
                } else if (type.equals(CemopEnum.ZERO_APPLICATION.getCode())) {
                    List<CemopZeroApplicationDTO> list = objectMapper.readerForListOf(CemopZeroApplicationDTO.class).readValue(str);
                    cemopDownDTO.setCemopZeroApplicationDTOS(list);
                    return cemopDownDTO;
                } else if (type.equals(CemopEnum.ALLOCATE_LEDGER_INFORMATION.getCode())) {
                    List<CemopAllocateLedgerInformationDTO> list = objectMapper.readerForListOf(CemopAllocateLedgerInformationDTO.class).readValue(str);
                    cemopDownDTO.setCemopAllocateLedgerInformationDTOS(list);
                    return cemopDownDTO;
                }else if (type.equals(CemopEnum.CANCEL_LEDGER_ALLOCATION.getCode())) {
                    List<CemopCancelLedgerAllocationDTO> list = objectMapper.readerForListOf(CemopCancelLedgerAllocationDTO.class).readValue(str);
                    cemopDownDTO.setCemopCancelLedgerAllocationDTOS(list);
                    return cemopDownDTO;
                }else if (type.equals(CemopEnum.CANCEL_LEDGER_OUTBOUND.getCode())) {
                    List<CemopCancelLedgerOutboundDTO> list = objectMapper.readerForListOf(CemopCancelLedgerOutboundDTO.class).readValue(str);
                    cemopDownDTO.setCemopCancelLedgerOutboundDTOS(list);
                    return cemopDownDTO;
                }else if (type.equals(CemopEnum.NEW_CEFCD_ECD.getCode())) {
                    List<NewCefcdecdDTO> list = objectMapper.readerForListOf(NewCefcdecdDTO.class).readValue(str);
                    cemopDownDTO.setNewCefcdecdDTOS(list);
                    return cemopDownDTO;
                }else if (type.equals(CemopEnum.SECOND_MATCH_AND_RETURN.getCode())){
                    List<SecondMatchAndReturnDTO> list = objectMapper.readerForListOf(SecondMatchAndReturnDTO.class).readValue(str);
                    cemopDownDTO.setSecondMatchAndReturnDTOS(list);
                }else if (type.equals(CemopEnum.CANCEL_DELIVER.getCode())){
                    List<CemopCancelDeliverDTO> list = objectMapper.readerForListOf(CemopCancelDeliverDTO.class).readValue(str);
                    cemopDownDTO.setCemopCancelDeliverDTOS(list);
                }else if (type.equals(CemopEnum.SORT_BACKR_ESULT_TXT.getCode())){
                    List<CemopSortBackResultTxtDTO> list = objectMapper.readerForListOf(CemopSortBackResultTxtDTO.class).readValue(str);
                    cemopDownDTO.setCemopSortBackResultTxtDTOS(list);
                }else if (type.equals(CemopEnum.LEDGER_OUTBOUND.getCode())) {
                    List<CemopLedgerOutboundDTO> list = objectMapper.readerForListOf(CemopLedgerOutboundDTO.class).readValue(str);
                    cemopDownDTO.setCemopLedgerOutboundDTOS(list);
                    return cemopDownDTO;
                }
            } catch (JsonProcessingException e) {
                log.info("解析失败");
                cemopDownDTO.setMessage("解析失败" + str);
                cemopDownDTO.setStatus("fail");
                throw new RuntimeException(e);
            } finally {
                return cemopDownDTO;
            }
        }
    }

    /**
     * 打包文件上传到Cemop
     *
     * @param dto
     * @param type
     */
    public JSONObject uploadUtil(CemopUploadDTO dto, String type) {
        String CemopUrl = "";
        JSONArray jsonArray = new JSONArray();
        ObjectMapper mapper = new ObjectMapper();
        try {
            if (type.equals(CemopEnum.INITIATE_COORDINATION.getCode())) {
                CemopInitiateCoordinationDTO cemopInitiateCoordinationDTO = dto.getCemopInitiateCoordinationDTO();
                Map<String, Object> map = new ObjectMapper().convertValue(cemopInitiateCoordinationDTO, Map.class);
                CemopUrl = environment.getProperty("cemopUrl.coordinateCreate");
                jsonArray.add(new JSONObject(map));
            } else if (type.equals(CemopEnum.ZERO_COORDINATION.getCode())) {
                CemopZeroCoordinationDTO cemopZeroCoordinationDTO = dto.getCemopZeroCoordinationDTO();
                Map<String, Object> map = new ObjectMapper().convertValue(cemopZeroCoordinationDTO, Map.class);
                CemopUrl = environment.getProperty("cemopUrl.coordinateEnd");
                jsonArray.add(new JSONObject(map));
            } else if (type.equals(CemopEnum.AO_SUPPORTING_LEDGER.getCode())) {
                CemopSupportingLedgerDTO cemopSupportingLedgerDTO = dto.getCemopSupportingLedgerDTO();
                Map<String, Object> map = new ObjectMapper().convertValue(cemopSupportingLedgerDTO, Map.class);
                CemopUrl = environment.getProperty("cemopUrl.aoMatch");
                jsonArray.add(new JSONObject(map));
                //新增库存确认
            } else if (type.equals(CemopEnum.STOCK_CONFIRMATION.getCode())) {
                CemopStockConfirmationDTO cemopStockConfirmationDTO = dto.getCemopStockConfirmationDTO();
                Map<String, Object> map = new ObjectMapper().convertValue(cemopStockConfirmationDTO, Map.class);
                CemopUrl = environment.getProperty("cemopUrl.aoMatch");
                jsonArray.add(new JSONObject(map));
            } else if (type.equals(CemopEnum.CANCEL_AO_SUPPORTING_LEDGER.getCode())) {
                CemopCancelSupportingLedgerDTO cemopCancelSupportingLedgerDTO = dto.getCemopCancelSupportingLedgerDTO();
                Map<String, Object> map = new ObjectMapper().convertValue(cemopCancelSupportingLedgerDTO, Map.class);
                CemopUrl = environment.getProperty("cemopUrl.cancelAoMatch");
                jsonArray.add(new JSONObject(map));
            } else if (type.equals(CemopEnum.AO_MATCHING_OUTBOUND.getCode())) {
                CemopAoMatchingOutboundDTO cemopAoMatchingOutboundDTO = dto.getCemopAoMatchingOutboundDTO();
                Map<String, Object> map = new ObjectMapper().convertValue(cemopAoMatchingOutboundDTO, Map.class);
                CemopUrl = environment.getProperty("cemopUrl.aoMatchOut");
                jsonArray.add(new JSONObject(map));
            } else if (type.equals(CemopEnum.AO_REVOKES_OUTBOUND.getCode())) {
                CemopAoRevokesOutboundDTO cemopAoRevokesOutboundDTO = dto.getCemopAoRevokesOutboundDTO();
                Map<String, Object> map = new ObjectMapper().convertValue(cemopAoRevokesOutboundDTO, Map.class);
                CemopUrl = environment.getProperty("cemopUrl.cancelAoMatchOut");
                jsonArray.add(new JSONObject(map));
            } else if (type.equals(CemopEnum.AO_SUPPORTING_TRANSFER.getCode())) {
                CemopAoSupportingTransferDTO cemopAoSupportingTransferDTO = dto.getCemopAoSupportingTransferDTO();
                Map<String, Object> map = new ObjectMapper().convertValue(cemopAoSupportingTransferDTO, Map.class);
                CemopUrl = environment.getProperty("cemopUrl.aoMatchDelivery");
                jsonArray.add(new JSONObject(map));
            }
            String path = compressedFilesEditor.generateZip(jsonArray.toString());
            String name = CemopEnum.getValueByCode(type);
            compressedFilesEditor.fileSend(jsonArray.toString(),path, CemopUrl, name);
            //用于记录日志
            HashMap<String, Object> hashMap=new HashMap<>();
            hashMap.put("filePathUrl",path);
            hashMap.put("CemopUrl",CemopUrl);
            hashMap.put("jsonArray",new JSONArray(jsonArray));
            JSONObject jsonObject = new JSONObject(hashMap);
            return jsonObject;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
